package design.mode.effective_java.chapter4.item23;

/**
 * 带标签的类——远远低于类层次结构!
 * 标签类冗长、容易出错和低效。
 * @author ZhongMingde on 2021/8/14
 */

class Figure {
    enum Shape {RECTANGLE,CIRCLE};

    // 标签字样, 图形的形状
    final Shape shape;

    // 这些字段仅仅是在形状是RECTANGLE被使用
    double length;

    double width;

    // 这些字段仅仅是在形状是CIRCLE被使用
    double radius;

    Figure(double radius){
        shape = Shape.CIRCLE;
        this.radius = radius;
    }

    Figure(double length,double width){
        shape = Shape.RECTANGLE;
        this.length = length;
        this.width = width;
    }

    double area(){
        switch (shape){
            case CIRCLE:
                return Math.PI * (radius * radius);
            case RECTANGLE:
                return length * width;
            default:
                throw new AssertionError(shape);
        }
    }
}
